package pack;
//20230404430920沈泽华
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Random rd = new Random();
        int random = rd.nextInt(99) + 1;
        Scanner scanner = new Scanner(System.in);
        boolean t = true;
        //捕获String转型int的异常处理
        System.out.println("猜一个1到100之间的数字:");
        while(t){
            try{
                String str= scanner.next();
                int num = Integer.parseInt(str);
                //String转int
                if(num == random){
                    System.out.println("你猜对了");
                    t = false;
                }
                else if (num > random && num <100)
                    System.out.println("太大了，再来");
                else if (num >1 && num < random)
                    System.out.println("太小了，再来");
                else if (num > 100 || num < 1)
                    System.out.println("输入的数字不在1到100间，重新输入");
            }catch(Exception e){
                e.printStackTrace();
                System.out.println("输入的不是一个有效数字，重新输入");
                scanner.nextLine();
            }
        }
    }
}
